<template>
    <div>
        <div class="mx-4 my-8 text-2xl">站点设置</div>
        <div class="flex">
            <div class="flex-1">
                <div class="m-4 mt-0 text-xl">案例问答大模型</div>
                <div class="m-4 pl-8">
                    <ModelConfigSelection
                        v-model="caseChatModel"
                        @submit="caseChatModelSubmit"
                    />
                </div>
                <div class="m-4 mt-8 text-xl">写作辅助大模型</div>
                <div class="m-4 pl-8">
                    <ModelConfigSelection
                        v-model="writingChatModel"
                        @submit="writingChatModelSubmit"
                    />
                </div>
            </div>
            <div class="flex-1">
                <div class="m-4 mt-0 text-xl">路径设置</div>
                <div class="m-4 pl-8">
                    <UrlSetting
                        v-model="urlConfigs"
                        @submit="urlConfigSubmit"
                    />
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getList, update } from '@/apis/commonCrud'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import ModelConfigSelection from './ModelConfigSelection.vue'
import UrlSetting from './UrlSetting.vue'

const caseChatModel = ref({
    id: 1,
    name: '加载中...',
    sdk: '',
    model: '',
    baseURL: '',
})
const writingChatModel = ref({
    id: 2,
    name: '加载中...',
    sdk: '',
    model: '',
    baseURL: '',
})

const urlConfigs = ref({
    uploadBaseUrl: '',
    chromaUrl: ''
})

let idMap = {}

const getData = async () => {
    const res = await getList('siteConfigs', {
        page: 1,
        pageSize: 100,
    })
    if (!res.success) return

    const keyMap: any = {}
    idMap = {}
    res.data.rows.forEach((item: any) => {
        keyMap[item.configKey] = item.configValue
        idMap[item.configKey] = item.id
    })
    if (keyMap['caseChatModel']) {
        caseChatModel.value = keyMap['caseChatModel']
    }
    if (keyMap['writingChatModel']) {
        writingChatModel.value = keyMap['writingChatModel']
    }
    if (keyMap['urlConfigs']) {
        urlConfigs.value = keyMap['urlConfigs']
    }
}

getData()

const updateConfig = async (id: number, value: any, done: () => void) => {
    const res = await update('siteConfigs', id, {
        configValue: value,
    })
    if (!res.success) {
        ElMessage.error('修改失败')
        return
    }
    ElMessage.success('修改成功')
    await getData()
    done()
}

const caseChatModelSubmit = async (value, done) => {
    const id = idMap['caseChatModel']
    updateConfig(id, value, done)
}
const writingChatModelSubmit = async (value, done) => {
    const id = idMap['writingChatModel']
    updateConfig(id, value, done)
}

const urlConfigSubmit = async (value, done) => {
    const id = idMap['urlConfigs']
    updateConfig(id, value, done)
}
</script>

<style scoped></style>
